package cn.tedu.egweibo11.controller;

import cn.tedu.egweibo11.mapper.WeiboMapper;
import cn.tedu.egweibo11.pojo.dto.WeiboDTO;
import cn.tedu.egweibo11.pojo.entity.Weibo;
import cn.tedu.egweibo11.pojo.vo.UserVO;
import cn.tedu.egweibo11.pojo.vo.WeiboDetailVO;
import cn.tedu.egweibo11.pojo.vo.WeiboVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {

    @Autowired
    private WeiboMapper weiboMapper;

    @PostMapping("insert")
    public int insertWeibo(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        /**
         * 发布微博功能
         * 1.确认登录状态
         * 2.获取用户ID
         * 3.插入weibo数据
         */
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO==null){
            return 2;
        }

        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());

        weiboMapper.insertWeibo(weibo);

        return 1;
    }

    @GetMapping("selectIndex")
    public List<WeiboVO> selectIndex (){
        return weiboMapper.selectIndex();
    }

    @GetMapping("selectById")
    public WeiboDetailVO selectById(Long wid){
        /**
         * 查看微博详情
         * 1.连接用户表以获nickname
         * 2.展示
         */


        return weiboMapper.selectById(wid);
    }
}
